package com.hex.liyue.lookingdatemodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.commonmodule.utils.L;
import com.hex.liyue.commonmodule.view.recycleview.YRecycleview;
import com.hex.liyue.lookingdatemodule.activity.InSomeDetailsActivity;
import com.hex.liyue.lookingdatemodule.adapter.AdapterHotNew;
import com.hex.liyue.lookingdatemodule.bean.HotNewResultBean;
import com.hex.liyue.lookingdatemodule.presenter.HotNewPresenter;
import com.hex.liyue.lookingdatemodule.presenter.IHotNewView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by WJ on 2016/6/8.
 */
public class FragmentHotNew extends BaseFragment implements YRecycleview.RefreshAndLoadMoreListener, AdapterHotNew.OnItemClickListener, IHotNewView {

    @Bind(R.id.rv_looking_date)
    YRecycleview rvLookingDate;
    private HotNewPresenter hotNewPresenter;
    private List<HotNewResultBean.DataBean> list = new ArrayList<>();
    private AdapterHotNew adapterHotNew;


    private String type;
    private int sex = 3;//1.女生 2.男生 3.全部
    private int page = 1;
    private int pagenum = 10;

    public static FragmentHotNew newInstance(String type) {
        Bundle bundle = new Bundle();
        FragmentHotNew fragmentHotNew = new FragmentHotNew();
        bundle.putString("type", type);
        fragmentHotNew.setArguments(bundle);

        return fragmentHotNew;
    }

    @Override
    public void initdata(Bundle arguments) {
        type = arguments.getString("type");

        layoutLoading();
        hotNewPresenter.searchAppointment(sex, type, page, pagenum);

        AdapterHotNew adapterHotNew = new AdapterHotNew(null);
        adapterHotNew.setOnItemClickListener(this);
        rvLookingDate.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvLookingDate.setAdapter(adapterHotNew);
        rvLookingDate.setRefreshAndLoadMoreListener(this);
    }

    public void setSex(int sex) {
        this.sex = sex;
        layoutLoading();
        hotNewPresenter.searchAppointment(sex, type, page, pagenum);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_hot_new;
    }

    @Override
    protected void bindPresenter() {
        hotNewPresenter = new HotNewPresenter(this);
    }

    @Override
    protected void initToolbar() {

    }

    @Override
    public void onRefresh() {
        page = 1;
        hotNewPresenter.searchAppointment(sex, type, page, pagenum);
    }

    @Override
    public void onLoadMore() {
        page++;
        hotNewPresenter.searchAppointment(sex, type, page, pagenum);
    }


    @Override
    public void onItemClickListener(String appointment_id) {
        Intent intent = new Intent(getActivity(), InSomeDetailsActivity.class);
        intent.putExtra("appointment_id", appointment_id);
        startActivity(intent);
    }

    @Override
    public void setAdapter(HotNewResultBean hotNewResultBean) {
        if (page == 1) {
            list.clear();
        }
        if (hotNewResultBean != null) {
            if (hotNewResultBean.data.size() == 0 && page > 1) {
                page--;
            }
            for (HotNewResultBean.DataBean dataBean : hotNewResultBean.data) {
                list.add(dataBean);
            }
        }

        if (adapterHotNew == null) {
            adapterHotNew = new AdapterHotNew(list);
            adapterHotNew.setOnItemClickListener(this);
            rvLookingDate.setLayoutManager(new LinearLayoutManager(getActivity()));
            rvLookingDate.setAdapter(adapterHotNew);
        }
        adapterHotNew.notifyDataSetChanged();
        rvLookingDate.reSetStatus();
        layoutContent();
        L.d("WJ", page + "=size=" + hotNewResultBean.data.size());
    }


    @Override
    public void showLod(String msg) {
        showLoad(getActivity(), msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }
}
